// weapp/fields_editor/searchSelect/searchSelect.js
import ex from '../../ex/index'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    head:{
      type:Object
    },
    row:{}
  },

  /**
   * 组件的初始数据
   */
  data: {
      options:[],
      show_label:'请选择',
      show:false,
      search_value:'',
      focus:false
  },
  observers:{
  },
  attached () {
    if(this.properties.head.options){
      this.setData({
        options:this.properties.head.options
      })
    }

  },
  /**
   * 组件的方法列表
   */
  methods: {
    clear(){
      this.setData({
        search_value:'',
        show_label:'请选择',
        options:[],
      })
      this.triggerEvent('input', null)
    },
    onSelect(e){
      var value = e.currentTarget.dataset.value
      this.triggerEvent('input', value)
      this.updateShowLabel(value)
      this.setData({
        show:false
      })
    },
    onValueChange(e){
      this.setData({
        search_value:e.detail
      })
      if(this.properties.head.onSearchInput){
        this.properties.head.onSearchInput(this)
      }
    },
    openSelect(){
      this.setData({
        show:true,
      })
      setTimeout(()=>{
        this.setData({
          focus:true
        })
        console.log('聚焦')
      },500)
    },
    onClickHide(){
      this.setData({
        show:false,
        focus:false
      })
    },
    updateShowLabel(nv){
      var one = ex.findone(this.data.options,{value:nv})
      if(one){
        var index = this.data.options.indexOf(one)
        this.setData({
          show_label:one.label,
        })
      } else{
        this.setData({
          show_label:nv,
        })
      }
      if(!nv){
        this.setData({
          show_label:'请选择'
        })
      }
    }
  }
})
